package com.wuchao.convert.controller;


import com.wuchao.convert.data.GetIpMain;
import com.wuchao.convert.entity.Device;
import com.wuchao.convert.exception.Result;
import com.wuchao.convert.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Map;

/**
 * @author WuChao
 * @version 1.0
 * @date 2020/4/3 11:08
 */
@RestController
@CrossOrigin
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    @GetMapping("/getDevice/{did}")
    public Device getDevice(@PathVariable(name = "did") String did) {
        Device device = deviceService.selectByPrimaryKey(did);
        return device;

    }

    @GetMapping("/getIp")
public String getIp() throws SocketException, UnknownHostException {

      return   GetIpMain.getIpAdd();

    }

    @GetMapping("/control/start/{did}")
    public Result startRm(@PathVariable(name = "did") String did) throws SocketException, UnknownHostException {

        Map<String, String> stringStringMap = deviceService.startConvert(did);
        return new Result("操作成功", 200, stringStringMap);
    }

    @GetMapping("/control/stop/{id}")
    public Result stop(@PathVariable(name = "id") String id) {
        Boolean aBoolean = deviceService.stopConvert(id);
        if (aBoolean) {
            return new Result("操作成功", 200, aBoolean);
        }else{
            return new Result("操作失败", 01111, aBoolean);
        }

    }

    @GetMapping("/control/replay/{did}/{startTime}/{endTime}")
    public Result replay(@PathVariable(name = "did") String did, @PathVariable(name = "startTime") String startTime, @PathVariable(name = "endTime") String endTime) {
        String convert = deviceService.replayConvert(did, startTime, endTime);
        return new Result("操作成功", 200, convert);
    }






}
